_script.js ➔ search   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 22
Code Lines 15

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 15
dl 22
loc 22
c 0
b 0
f 0
rs 5.9999
cc 10

How to fix   Complexity   

Complexity

Complex classes like _script.js ➔ search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1 View Code Duplication
$('i.glyphicon-refresh-animate').hide();
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
function updateItems(r) {
3
    _opts.items.available = r.available;
0 ignored issues
show
Bug introduced by
The variable _opts seems to be never declared. If this is a global, consider adding a /** global: _opts */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
    _opts.items.assigned = r.assigned;
5
    search('available');
6
    search('assigned');
7
}
8
9
$('.btn-assign').click(function () {
10
    var $this = $(this);
11
    var target = $this.data('target');
12
    var items = $('select.list[data-target="' + target + '"]').val();
13
14
    if (items && items.length) {
15
        $this.children('i.glyphicon-refresh-animate').show();
16
        $.post($this.attr('href'), {items: items}, function (r) {
17
            updateItems(r);
18
        }).always(function () {
19
            $this.children('i.glyphicon-refresh-animate').hide();
20
        });
21
    }
22
    return false;
23
});
24
25
$('.search[data-target]').keyup(function () {
26
    search($(this).data('target'));
27
});
28
29
function search(target) {
30
    var $list = $('select.list[data-target="' + target + '"]');
31
    $list.html('');
32
    var q = $('.search[data-target="' + target + '"]').val();
33
34
    var groups = {
35
        role: [$('<optgroup label="Roles">'), false],
36
        permission: [$('<optgroup label="Permission">'), false],
37
        route: [$('<optgroup label="Routes">'), false],
38
    };
39
    $.each(_opts.items[target], function (name, group) {
0 ignored issues
show
Bug introduced by
The variable _opts seems to be never declared. If this is a global, consider adding a /** global: _opts */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
        if (name.indexOf(q) >= 0) {
41
            $('<option>').text(name).val(name).appendTo(groups[group][0]);
42
            groups[group][1] = true;
43
        }
44
    });
45
    $.each(groups, function () {
46
        if (this[1]) {
47
            $list.append(this[0]);
48
        }
49
    });
50
}
51
52
// initial
53
search('available');
54
search('assigned');
55